import request from '@/utils/axios'
import { getRefreshToken } from '@/utils/auth'
import type { UserLoginType, UserType } from './types'
import type { MenuType } from '../system/menu'

// 短信登录验证码获取
export interface SmsCodeType {
  mobile: string
  scene?: number
}

// 短信登录
export interface SmsLoginType {
  mobile: string
  code: string
}

export interface UserInfoType {
  user: UserType
  permit: number[]
  menus: MenuType[]
}

// 权鉴登录验证相关
export const AuthApi = {
  // 登录
  signIn: (data: UserLoginType) => {
    return request.post({ url: '/system/auth/login', data, is_mock: true })
  },
  // 登出
  signOut: () => {
    return request.post({ url: '/system/auth/logout', is_mock: true })
  },
  // 用户重置密码
  resetPassword: data => {
    return request.postOriginal({ url: '/system/auth/user-reset', data, is_mock: true })
  },
  // 刷新访问令牌
  refreshToken: () => {
    return request.post({
      url: '/system/auth/refresh-token',
      data: {
        refreshToken: getRefreshToken()
      },
      is_mock: true
    })
  },
  // 获取信息相关
  info: (): Promise<UserInfoType> => {
    return request.get({ url: '/system/auth/get-user-info', is_mock: true })
  },
  // 获取通知消息相关
  notice: () => {
    return request.get({ url: '/system/auth/get-notice-info', is_mock: true })
  },
  // 短信验证码登录
  Sms: {
    // 获取登录验证码
    code: (data: SmsCodeType) => {
      return request.post({ url: '/system/auth/get-permission-info', data, is_mock: true })
    },
    // 短信验证码登录
    signIn: (data: SmsLoginType) => {
      return request.post({ url: '/system/auth/sms-login', data, is_mock: true })
    }
  },
  // 社交账号登录
  Social: {
    // 社交账号快捷登录，使用 code 授权码
    signIn: (type: string, code: string, state: string) => {
      return request.post({ url: '/system/auth/social-login', data: { type, code, state }, is_mock: true })
    },
    // 社交账号授权的跳转
    authRedirect: (type: number, redirectUri: string) => {
      return request.get({
        url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri,
        is_mock: true
      })
    }
  },
  // 验证码验证相关
  Captcha: {
    // 获取验证码图片及 token
    code: data => {
      return request.postOriginal({ url: '/system/captcha/get', data, is_mock: true })
    },
    // 滑动或者点选验证
    check: data => {
      return request.postOriginal({ url: '/system/captcha/check', data, is_mock: true, is_custom_error: true })
    }
  },
  // 租户相关
  Tenant: {
    // 使用租户名获取租户编号
    getIdByName: (name: string) => {
      return request.get({ url: '/system/tenant/get-id-by-name?name=' + name, is_mock: true })
    },
    // 使用租户域名获取租户信息
    getInfoByDomain: (domain: string) => {
      return request.get({ url: '/system/tenant/get-info-by-domain?domain=' + domain, is_mock: true })
    }
  }
}

// 应用授权登录相关
export const OAuth2Api = {
  // 获取授权信息
  getAuthorize: (clientId: string) => {
    return request.get({ url: '/system/oauth2/authorize?clientId=' + clientId, is_mock: true })
  },
  // 发起授权
  authorize: (responseType: string, clientId: string, redirectUri: string, state: string, autoApprove: boolean, checkedScopes: string[], uncheckedScopes: string[]) => {
    // 构建 scopes
    const scopes = {}
    for (const scope of checkedScopes) {
      scopes[scope] = true
    }
    for (const scope of uncheckedScopes) {
      scopes[scope] = false
    }
    return request.post({
      url: '/system/oauth2/authorize',
      headers: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        response_type: responseType,
        client_id: clientId,
        redirect_uri: redirectUri,
        state: state,
        auto_approve: autoApprove,
        scope: JSON.stringify(scopes)
      },
      is_mock: true
    })
  }
}
